package entity;

import java.io.UnsupportedEncodingException;

import Util.Util;
import messagePacket.AddFriendPacket;
import messagePacket.DeleteFriendPacket;
import messagePacket.JoinGroupPacket;
import messagePacket.MessagePacket;
import messagePacket.Packet;
import messagePacket.SearchFriendPacket;

public class PacketFactory {

	public static Packet createPacketFromBuffer(byte[] buffer) throws PacketDecodeErrorException, UnsupportedEncodingException {
		int packetType = Util.bytes2int(buffer, 0);
		System.out.println("packetType--->:" + packetType);
		if (packetType == Util.PACKET_MESSAGE) {
			MessagePacket packet = new MessagePacket();
			packet.decode(buffer);
			return packet;
		} else if (packetType == Util.PACKET_ADD_FRIEND) {
			Packet packet = new AddFriendPacket();
			packet.decode(buffer);
			return packet;
		} else if (packetType == Util.PACKET_JOIN_GROUP) {
			Packet packet = new JoinGroupPacket();
			packet.decode(buffer);
			return packet;
		} else if (packetType == Util.PACKET_SEARCH_FRIEND) {
			Packet packet = new SearchFriendPacket();
			packet.decode(buffer);
			return packet;
		}else if (packetType == Util.PACKET_DELETE_FRIEND) {
			Packet packet = new DeleteFriendPacket();
			packet.decode(buffer);
			return packet;
		}
		throw new PacketDecodeErrorException("packet not exists");		
	}
}
